5 * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * * Neither the name of Sebastian Bergmann nor the names of his
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
40 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 * @link http://www.phpunit.de/
44 * @since File available since Release 2.0.0
47 require_once 'PHPUnit/Framework.php';
48 require_once 'PHPUnit/Util/Filter.php';
50 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
52 if (!class_exists('PHPUnit_Framework_TestFailure', FALSE)) {
55 * A TestFailure collects a failed test together with the caught exception.
59 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
60 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
61 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
62 * @version Release: 3.3.17
63 * @link http://www.phpunit.de/
64 * @since Class available since Release 2.0.0
66 class PHPUnit_Framework_TestFailure
69 * @var PHPUnit_Framework_Test
71 protected $failedTest;
76 protected $thrownException;
79 * Constructs a TestFailure with the given test and exception.
81 * @param PHPUnit_Framework_Test $failedTest
82 * @param Exception $thrownException
84 public function __construct(PHPUnit_Framework_Test $failedTest, Exception $thrownException)
86 $this->failedTest = $failedTest;
87 $this->thrownException = $thrownException;
91 * Returns a short description of the failure.
95 public function toString()
101 $this->thrownException->getMessage()
106 * Returns a verbose description of the failure.
108 * @param bool $verbose
110 * @since Method available since Release 3.2.0
112 public function toStringVerbose($verbose = FALSE)
114 return self::exceptionToString($this->thrownException, $verbose);
118 * Returns a verbose description for an exception.
120 * @param Exception $e
121 * @param bool $verbose
123 * @since Method available since Release 3.2.0
125 public static function exceptionToString(Exception $e, $verbose = FALSE)
127 if ($e instanceof PHPUnit_Framework_SelfDescribing) {
128 if ($e instanceof PHPUnit_Framework_ExpectationFailedException) {
129 $comparisonFailure = $e->getComparisonFailure();
130 $description = $e->getDescription();
131 $message = $e->getCustomMessage();
133 if ($message == '') {
136 $buffer = $message . "\n";
139 if ($comparisonFailure !== NULL) {
140 if ($comparisonFailure->identical()) {
141 if ($comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Object) {
142 $buffer .= "Failed asserting that two variables reference the same object.\n";
144 $buffer .= $comparisonFailure->toString() . "\n";
147 if ($comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Scalar) {
149 "Failed asserting that %s matches expected value %s.\n",
151 PHPUnit_Util_Type::toString($comparisonFailure->getActual()),
152 PHPUnit_Util_Type::toString($comparisonFailure->getExpected())
156 else if ($comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Array ||
157 $comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Object ||
158 $comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_String) {
160 "Failed asserting that two %ss are equal.\n%s\n",
162 strtolower(substr(get_class($comparisonFailure), 36)),
163 $comparisonFailure->toString()
168 !$comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Array &&
169 !$comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_Object &&
170 !$comparisonFailure instanceof PHPUnit_Framework_ComparisonFailure_String) {
171 $buffer .= $comparisonFailure->toString() . "\n";
175 $buffer .= $e->toString();
176 $equal = $buffer == $description;
178 if (!empty($buffer)) {
183 $buffer .= $description . "\n";
189 $buffer = $e->toString();
191 if (!empty($buffer)) {
197 else if ($e instanceof PHPUnit_Framework_Error) {
198 $buffer = $e->getMessage() . "\n";
202 $buffer = get_class($e) . ': ' . $e->getMessage() . "\n";
209 * Gets the failed test.
213 public function failedTest()
215 return $this->failedTest;
219 * Gets the thrown exception.
223 public function thrownException()
225 return $this->thrownException;
229 * Returns the exception's message.
233 public function exceptionMessage()
235 return $this->thrownException()->getMessage();
239 * Returns TRUE if the thrown exception
240 * is of type AssertionFailedError.
244 public function isFailure()
246 return ($this->thrownException() instanceof PHPUnit_Framework_AssertionFailedError);